import axios from "axios";
import {getToken} from "./auth";
import { Toast } from 'vant';
const baseURL = "http://www.huixh.cn/api";

axios.defaults.headers['Content-Type'] = 'application/json';
const request = axios.create({
    baseURL,
    timeout: 10000,
});

/**
 * 请求拦截器
 */
request.interceptors.request.use(
    config => {
        config.headers['X-Token'] = getToken();
        return config;
    },
    error => {
        console.log(error)
        return Promise.reject(error);
    }
);

/**
 * 响应拦截器
 */
request.interceptors.response.use(
    response => {
        return response.data;
    }
    ,
    error => {
        if (error.message.includes("timeout")) {
            Toast.fail('请求超时！');
        }
        return Promise.reject(error);
    }
);

export default request;

